package com.heima.article.listener;

import com.alibaba.fastjson.JSON;
import com.heima.article.entity.ApArticle;
import com.heima.article.service.ApArticleService;
import com.heima.common.constants.message.WmNewsUpDownConstants;
import com.heima.model.article.dtos.WmNewsDownOrUpMsgDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class WmNewsDownOrUpListener {

    @Autowired
    private ApArticleService articleService;

    @KafkaListener(topics = WmNewsUpDownConstants.WM_NEWS_UP_OR_DOWN_TOPIC)
    public void onMessage(ConsumerRecord<String, String> consumerRecord){
        if(consumerRecord == null){
            return;
        }
        log.info("收到了自媒体文章上下架的消息:{}", consumerRecord.value());
        //更新article文章的上下架的状态
        WmNewsDownOrUpMsgDTO wmNewsDownOrUpMsgDTO = JSON.toJavaObject(JSON.parseObject(consumerRecord.value()), WmNewsDownOrUpMsgDTO.class);
        ApArticle toBeUpdate = new ApArticle();
        toBeUpdate.setId(wmNewsDownOrUpMsgDTO.getArticleId());
        toBeUpdate.setIsDown(!wmNewsDownOrUpMsgDTO.getEnable());
        articleService.updateById(toBeUpdate);
    }

}
